//
//  NoticeCell.m
//  wtshoppingcite
//
//  Created by TW on 15-1-12.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "NoticeCell.h"

@implementation NoticeCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}


- (CGFloat)getCellHight
{
    
    NSString* content =self.communityModel.outline;
    NSString* title = self.communityModel.title;
    NSString* time = self.communityModel.createTime;
    CGFloat titleHeight = [UIUtility getHeightOfString:title withFont:[UIFont systemFontOfSize:16.0f] withWidth:300];
    CGFloat timeHeight = [UIUtility getHeightOfString:time withFont:[UIFont systemFontOfSize:12.0f] withWidth:300];
    CGFloat contentHeight = [UIUtility getHeightOfString:content withFont:[UIFont systemFontOfSize:14.0f] withWidth:300];
    if(contentHeight>100){
        contentHeight = 80;
    }
    float h = titleHeight + timeHeight +contentHeight + 30;
    return h;
}

-(void)createSubViews{
    
    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"not_background"]];
    
    //为cell添加气泡效果背景
   
    _cellImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
   [self.contentView addSubview:_cellImageView];
    
    
    _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];
    _iconImageView.image =[UIImage imageNamed:@"tongzhixiaotubiao"];
    [_cellImageView addSubview:_iconImageView];
    
    _titleLabel = [self createLabelWithFrame:CGRectMake(_iconImageView.right + 5, 10, 220, 20) text:@"" font:16 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
    [_cellImageView addSubview:_titleLabel];
    
    _dayLabel = [self createLabelWithFrame:CGRectMake(_titleLabel.left, _titleLabel.bottom, 120, 20) text:@"" font:12 textAli:NSTextAlignmentLeft textColor:MAIN_GRAY_COLOR];
    [_cellImageView addSubview:_dayLabel];
    
    UIView *seperateView = [[UIView alloc]initWithFrame:CGRectMake(30, _dayLabel.bottom, kWidth-60, 0.5)];
    seperateView.backgroundColor = MAIN_GRAY_COLOR;
    [_cellImageView addSubview:seperateView];
    
    _subtitleLabel = [self createLabelWithFrame:CGRectMake(30, _dayLabel.bottom + 3, kWidth-60, 80) text:@"" font:14 textAli:NSTextAlignmentLeft textColor:MAIN_GRAY_COLOR];
    _subtitleLabel.numberOfLines = 4;
    [_cellImageView addSubview:_subtitleLabel];
    
}


-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
    if(text == nil)
        text = @"";
    UILabel* label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = alig;
    label.textColor = tc;
    label.backgroundColor = [UIColor clearColor];
    return label;
}

-(void)setCommunityModel:(CommunityModel *)communityModel{
    if (_communityModel != communityModel) {
        _communityModel = communityModel;
        
         CGFloat cellHeight = [self getCellHight];
        _cellImageView.frame = CGRectMake(5, 0, kWidth - 10, cellHeight+10);

        _cellImageView.image = [[UIImage imageNamed:@"kuang.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:30];

        _titleLabel.text = communityModel.title;
        
        _subtitleLabel.text = communityModel.outline;
        CGFloat height = [UIUtility getHeightOfString:_subtitleLabel.text withFont:[UIFont systemFontOfSize:15.0f] withWidth:300];
        if (height >=100) {
            height = 80;
        }
        _subtitleLabel.height = height;
       
        _dayLabel.text = communityModel.createTime;
        
    }
}

- (void)awakeFromNib {
    // Initialization code
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
